import asyncio

from loguru import logger

from com.arcfox.base.base_spider import BaseSpider, ExecuteType
from com.arcfox.middleware.async_redis_middleware import open_redis
from com.arcfox.school.processor.handcollege.hand_major_worker_processor import HandMajorWorkerProcessor
from com.arcfox.util import async_request as requests


class HandMajorWorkerSpider(BaseSpider):
    def __init__(self):
        super().__init__()
        self.url = "https://static-data.gaokao.cn/www/2.0/special/4/pc_special_detail.json"
        self.processor = HandMajorWorkerProcessor()

    @open_redis
    async def init_data_version(self, client):
        return await super().init_data_version(client)

    async def _pull_task(self):
        return None, ExecuteType.ONCE

    async def _crawl_by_task(self, tasks):
        for task in tasks:
            resp = await requests.get_with_session(self.url, self.get_default_header(), timeout=3)
            if resp.code == 200:
                logger.info(resp.json())
                result_json = resp.json()
                if result_json['code'] == '0000':
                    await self.processor.parse_and_save_data(task, result_json)



if __name__ == "__main__":
    asyncio.run(HandMajorWorkerSpider().start_crawl())